package org.zty.service;

import java.util.ArrayList;
import java.util.List;

import org.zty.domain.Attr;
import org.zty.domain.Pojo;

public class ToServiceStr {
	private static final String exception = "ZException";
	private static final String exceptionImport ="import org.zty.util.ZException;\r\n";
	private static final String importstr="import java.util.List;\r\nimport org.zty.common.BaseService;\r\nimport org.zty.db.DBMgr;\r\nimport java.sql.SQLException;\r\n";
	public String toService(Pojo pojo){
		StringBuffer sb = new StringBuffer();
		sb.append(importstr);
		sb.append(exceptionImport);
		sb.append("public class "+pojo.getName()+"Service extends BaseService {\r\n");
		sb.append(otherStr(pojo));
		sb.append(insertPojo(pojo));
		sb.append(deletePojo(pojo));
		sb.append(updatePojo(pojo));
		sb.append(getPojo(pojo));
		sb.append(findPojoTotal(pojo));
		sb.append(findAllPojo(pojo));
		sb.append(findListPojo(pojo));
		sb.append(findPaginationListPojo(pojo));
		sb.append("}");
		return sb.toString();
	}

	public String insertPojo(Pojo pojo){
		StringBuffer sb = new StringBuffer();
		List<Attr> attrList = pojo.getAttrList();
		sb.append("\tpublic void insert"+pojo.getName()+"("+pojo.getName()+" "+getLowcaseName(pojo)+") throws "+exception+"{\r\n");
		sb.append("\t\ttry {\r\n");
		sb.append("\t\t\tthis.client.insert(\"insert"+pojo.getName()+"\","+getLowcaseName(pojo)+");\r\n");
		sb.append("\t\t} catch (SQLException e) {\r\n");
		sb.append("\t\t\te.printStackTrace();\r\n");
		sb.append("\t\t\tthrow new "+exception+"(\"新增"+pojo.getName()+"失败\",e);\r\n");
		sb.append("\t\t} catch (Exception e) {\r\n");
		sb.append("\t\t\te.printStackTrace();\r\n");
		sb.append("\t\t\tthrow new "+exception+"(\"新增"+pojo.getName()+"失败\",e);\r\n");
		sb.append("\t\t}\r\n");
		sb.append("\t}\r\n");
		return sb.toString();
	}
	
	public String updatePojo(Pojo pojo){
		StringBuffer sb = new StringBuffer();
		List<Attr> attrList = pojo.getAttrList();
		sb.append("\tpublic void update"+pojo.getName()+"("+pojo.getName()+" "+getLowcaseName(pojo)+") throws "+exception+"{\r\n");
		sb.append("\t\ttry {\r\n");
		sb.append("\t\t\tthis.client.update(\"update"+pojo.getName()+"\","+getLowcaseName(pojo)+");\r\n");
		sb.append("\t\t} catch (SQLException e) {\r\n");
		sb.append("\t\t\te.printStackTrace();\r\n");
		sb.append("\t\t\tthrow new "+exception+"(\"修改"+pojo.getName()+"失败\",e);\r\n");
		sb.append("\t\t} catch (Exception e) {\r\n");
		sb.append("\t\t\te.printStackTrace();\r\n");
		sb.append("\t\t\tthrow new "+exception+"(\"修改"+pojo.getName()+"失败\",e);\r\n");
		sb.append("\t\t}\r\n");
		sb.append("\t}\r\n");
		return sb.toString();
	}
	
	public String deletePojo(Pojo pojo){
		StringBuffer sb = new StringBuffer();
		List<Attr> attrList = pojo.getAttrList();
		sb.append("\tpublic void delete"+pojo.getName()+"("+pojo.getName()+" "+getLowcaseName(pojo)+") throws "+exception+"{\r\n");
		sb.append("\t\ttry {\r\n");
		sb.append("\t\t\tthis.client.delete(\"delete"+pojo.getName()+"\","+getLowcaseName(pojo)+");\r\n");
		sb.append("\t\t} catch (SQLException e) {\r\n");
		sb.append("\t\t\te.printStackTrace();\r\n");
		sb.append("\t\t\tthrow new "+exception+"(\"删除"+pojo.getName()+"失败\",e);\r\n");
		sb.append("\t\t} catch (Exception e) {\r\n");
		sb.append("\t\t\te.printStackTrace();\r\n");
		sb.append("\t\t\tthrow new "+exception+"(\"删除"+pojo.getName()+"失败\",e);\r\n");
		sb.append("\t\t}\r\n");
		sb.append("\t}\r\n");
		return sb.toString();
	}
	
	
	public String getPojo(Pojo pojo){
		StringBuffer sb = new StringBuffer();
		List<Attr> attrList = pojo.getAttrList();
		sb.append("\tpublic "+pojo.getName()+" get"+pojo.getName()+"("+pojo.getName()+" "+getLowcaseName(pojo)+") throws "+exception+"{\r\n");
		sb.append("\t\ttry {\r\n");
		sb.append("\t\t\treturn ("+pojo.getName()+")this.client.queryForObject(\"find"+pojo.getName()+"\","+getLowcaseName(pojo)+");\r\n");
		sb.append("\t\t} catch (SQLException e) {\r\n");
		sb.append("\t\t\te.printStackTrace();\r\n");
		sb.append("\t\t\tthrow new "+exception+"(\"获取单"+pojo.getName()+"失败\",e);\r\n");
		sb.append("\t\t} catch (Exception e) {\r\n");
		sb.append("\t\t\te.printStackTrace();\r\n");
		sb.append("\t\t\tthrow new "+exception+"(\"获取单"+pojo.getName()+"失败\",e);\r\n");
		sb.append("\t\t}\r\n");
		sb.append("\t}\r\n");
		return sb.toString();
	}
	
	public String findPojoTotal(Pojo pojo){
		StringBuffer sb = new StringBuffer();
		List<Attr> attrList = pojo.getAttrList();
		sb.append("\tpublic int find"+pojo.getName()+"Total("+pojo.getName()+" "+getLowcaseName(pojo)+") throws "+exception+"{\r\n");
		sb.append("\t\ttry {\r\n");
		sb.append("\t\t\treturn (int)this.client.queryForObject(\"find"+pojo.getName()+"Total\","+getLowcaseName(pojo)+");\r\n");
		sb.append("\t\t} catch (SQLException e) {\r\n");
		sb.append("\t\t\te.printStackTrace();\r\n");
		sb.append("\t\t\tthrow new "+exception+"(\"获取总数"+pojo.getName()+"失败\",e);\r\n");
		sb.append("\t\t} catch (Exception e) {\r\n");
		sb.append("\t\t\te.printStackTrace();\r\n");
		sb.append("\t\t\tthrow new "+exception+"(\"获取总数"+pojo.getName()+"失败\",e);\r\n");
		sb.append("\t\t}\r\n");
		sb.append("\t}\r\n");
		return sb.toString();
	}
	
	public String findPaginationListPojo(Pojo pojo){
		StringBuffer sb = new StringBuffer();
		List<Attr> attrList = pojo.getAttrList();
		sb.append("\tpublic List<"+pojo.getName()+"> find"+pojo.getName()+"PaginationList("+pojo.getName()+" "+getLowcaseName(pojo)+") throws "+exception+"{\r\n");
		sb.append("\t\ttry {\r\n");
		sb.append("\t\t\treturn this.client.queryForList(\"findPaginationList"+pojo.getName()+"\","+getLowcaseName(pojo)+");\r\n");
		sb.append("\t\t} catch (SQLException e) {\r\n");
		sb.append("\t\t\te.printStackTrace();\r\n");
		sb.append("\t\t\tthrow new "+exception+"(\"获取物理分页"+pojo.getName()+"失败\",e);\r\n");
		sb.append("\t\t} catch (Exception e) {\r\n");
		sb.append("\t\t\te.printStackTrace();\r\n");
		sb.append("\t\t\tthrow new "+exception+"(\"获取物理分页"+pojo.getName()+"失败\",e);\r\n");
		sb.append("\t\t}\r\n");
		sb.append("\t}\r\n");
		return sb.toString();
	}
	
	public String findListPojo(Pojo pojo){
		StringBuffer sb = new StringBuffer();
		List<Attr> attrList = pojo.getAttrList();
		sb.append("\tpublic List<"+pojo.getName()+"> find"+pojo.getName()+"List("+pojo.getName()+" "+getLowcaseName(pojo)+") throws "+exception+"{\r\n");
		sb.append("\t\ttry {\r\n");
		sb.append("\t\t\treturn this.client.queryForList(\"find"+pojo.getName()+"\","+getLowcaseName(pojo)+","+getLowcaseName(pojo)+".getSkipSize(),"+getLowcaseName(pojo)+".getPageSize());\r\n");
		sb.append("\t\t} catch (SQLException e) {\r\n");
		sb.append("\t\t\te.printStackTrace();\r\n");
		sb.append("\t\t\tthrow new "+exception+"(\"获取"+pojo.getName()+"列表失败\",e);\r\n");
		sb.append("\t\t} catch (Exception e) {\r\n");
		sb.append("\t\t\te.printStackTrace();\r\n");
		sb.append("\t\t\tthrow new "+exception+"(\"获取"+pojo.getName()+"列表失败\",e);\r\n");
		sb.append("\t\t}\r\n");
		sb.append("\t}\r\n");
		return sb.toString();
	}
	
	
	public String findAllPojo(Pojo pojo){
		StringBuffer sb = new StringBuffer();
		List<Attr> attrList = pojo.getAttrList();
		sb.append("\tpublic List<"+pojo.getName()+"> findAll"+pojo.getName()+"("+pojo.getName()+" "+getLowcaseName(pojo)+") throws "+exception+"{\r\n");
		sb.append("\t\ttry {\r\n");
		sb.append("\t\t\treturn this.client.queryForList(\"find"+pojo.getName()+"\","+getLowcaseName(pojo)+");\r\n");
		sb.append("\t\t} catch (SQLException e) {\r\n");
		sb.append("\t\t\te.printStackTrace();\r\n");
		sb.append("\t\t\tthrow new "+exception+"(\"获取所有"+pojo.getName()+"失败\",e);\r\n");
		sb.append("\t\t} catch (Exception e) {\r\n");
		sb.append("\t\t\te.printStackTrace();\r\n");
		sb.append("\t\t\tthrow new "+exception+"(\"获取所有"+pojo.getName()+"失败\",e);\r\n");
		sb.append("\t\t}\r\n");
		sb.append("\t}\r\n");
		return sb.toString();
	}
	
	public String otherStr(Pojo pojo){
		StringBuffer sb = new StringBuffer();
		sb.append("\tpublic "+pojo.getName()+"Service() {\r\n");
		sb.append("\t\tthis.client = DBMgr.getSqlMapClient();\r\n");
		sb.append("\t}\r\n");
		return sb.toString();
	}
	
	
	public String getLowcaseName(String str){
		return str.substring(0,1).toLowerCase() + str.substring(1);
	}
	
	public String getLowcaseName(Pojo pojo){
		return getLowcaseName(pojo.getName());
	}
	
	public static void main(String[] args) {
		ToServiceStr ts = new ToServiceStr();
		Pojo p = new Pojo();
		p.setName("DocItem");
		Attr attr1=  new Attr();
		attr1.setName("id");
		attr1.setType(2);
		Attr attr2=  new Attr();
		attr2.setName("doctitle");
		attr2.setType(1);
		Attr attr3=  new Attr();
		attr3.setName("subtitle");
		attr3.setType(1);
		Attr attr4=  new Attr();
		attr4.setName("htmlcontent");
		attr4.setType(1);
		Attr attr5=  new Attr();
		attr5.setName("crtime");
		attr5.setType(3);
		Attr attr6=  new Attr();
		attr6.setName("doctime");
		attr6.setType(3);
		List l = new ArrayList<Attr>();
		l.add(attr1);
		l.add(attr2);
		l.add(attr3);
		l.add(attr4);
		l.add(attr5);
		l.add(attr6);
		p.setAttrList(l);
		System.out.println(ts.toService(p));
		
	}
}
